<?php

namespace App\Jobs;

use App\Models\Bill;
use App\Models\Wallet;
use App\Models\WithdrawOrder;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class WithdrawJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $withdraw;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(WithdrawOrder $withdraw)
    {
        //
        $this->withdraw = $withdraw;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 发起转账
        if ($this->withdraw->channel === 'WECHAT') {
            // 微信转账
        }elseif($this->withdraw->channel === 'ALIPAY'){
            // 支付宝转账
        }
        // 成功后记录流水
        $bill = [
            'origin_order_id'=>$this->withdraw->id,
            'fee'=>$this->withdraw->amount,
            'member_id'=>$this->withdraw->member_id,
            'type'=>'OUTPUT',
            'remark'=>'提现'
        ];
        Bill::create($bill);
        // 解除锁定
        $wallet = Wallet::where('member_id',$this->withdraw->member_id);
        $wallet->lock_blance = $wallet->lock_blance - $this->withdraw->amount;
        $wallet->save();
    }
}
